package cate.game.play.skill.passive.furniture;

import cate.common.table.d.GDPlay;
import cate.game.play.buff.Buff;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.skill.FighterCD;
import cate.game.play.skill.passive.PassiveHandler;
import easy.java.struct.ref.DoubleRef;

/**
 * 回合开始前为自身附加【血护】，2回合内受到的伤害超过40%，则提供30%最大生命值的护盾3回合，护盾每回合减少10%，冷却4回合
 */
public class 血护之盾PH extends PassiveHandler {

	private int 检测回合;

	private double 伤害比例;

	private int 护盾buff;

	private double 减少系数;

	private int 冷却回合;

	//检测回合=2&伤害比例=4000&护盾buff=？？&减少系数=1000&冷却回合=4
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		检测回合 = args.getInt("检测回合", 0);
		伤害比例 = args.getWDouble("伤害比例", 0d);
		护盾buff = args.getInt("护盾buff", 0);
		减少系数 = args.getWDouble("减少系数", 0d);
		冷却回合 = args.getInt("冷却回合", 0);
	}


	//开始统计的回合
	private int startRound = 1;
	//上次添加buff的回合
	private int settleRound;
	//护盾buffPid
	private int buffPid;

	@Override
	public void onRoundEnd(ActionCtx action) {
		if (buffPid > 0) {
			Buff shieldBuff = skill.owner.buff.findOneByPid(buffPid);
			if (shieldBuff == null) {
				return;
			}
			shieldBuff.handler.handleAbsorb(action, new DoubleRef(skill.owner.attr.total().hpTotal() * 减少系数));
		}
	}


	@Override
	public void onMoveDone(MoveCtx move, TimeActionCtx action) {
		if (FighterCD.inCD(settleRound, 冷却回合, move.getRound().index)) {
			return;
		}
		if (skill.owner.isDead()) {
			return;
		}
		double hurt = calHurt(startRound, move.getRound().index);
		if (hurt <= skill.owner.attr.total().hpTotal() * 伤害比例) {
			return;
		}
		settleRound = move.getRound().index;
		startRound = settleRound + 冷却回合 + 1;
		buffPid = skill.owner.buff.tryAddByTid(action, 护盾buff);
	}

	private double calHurt(int startRound, int curRound) {
		startRound = Math.max(startRound, curRound - (检测回合 - 1));
		return skill.owner.performance.calTotalHurt(startRound, curRound);
	}
}
